package com.nerve.android.cloudoffice.app.activity;

import java.util.HashMap;
import java.util.Map;

import org.nerve.android.NerveActivityGroup;
import org.nerve.android.annotation.Acvitity;
import org.nerve.android.annotation.ViewOnId;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.nerve.android.cloudoffice.OfficeApplication;
import com.nerve.android.cloudoffice.R;
import com.nerve.android.cloudoffice.app.NavigationActivity;
import com.nerve.android.cloudoffice.app.group.IndexGroupActivity;
import com.nerve.android.cloudoffice.common.Const;
import com.nerve.android.cloudoffice.common.Reply;
import com.nerve.android.cloudoffice.domain.User;
import com.nerve.android.cloudoffice.service.NetService;

/**
 * 
 * @project Cloudoffice StartupActivity.java
 * @datetime Aug 12, 2013 10:46:46 AM
 * @description 
 * 	首页activity   
 * @author 集成显卡 IBM 
 *			zxingming@qq.com
 *			zengxm@cn.ibm.com
 *
 * @change log:
 *
 */
@SuppressLint("HandlerLeak")
@Acvitity(layout=R.layout.include_index_menu_black)
public class StartupActivity extends NavigationActivity implements View.OnClickListener{
	
	private Map<String, Integer> countMap = new HashMap<String, Integer>();

	/*
	 * 首页的菜单按键
	 */
	@ViewOnId(id=R.id.menuBus, clickListener="this")
	protected View menuBus;
	@ViewOnId(id=R.id.menuHotel, clickListener="this")
	protected View menuHotel;
	@ViewOnId(id=R.id.menuTravel, clickListener="this")
	protected View menuTravel;
	@ViewOnId(id=R.id.menuCustomer, clickListener="this")
	protected View menuMember;
	@ViewOnId(id=R.id.menuTrashApply, clickListener="this")
	protected View menuTrash;
	@ViewOnId(id=R.id.menuMark, clickListener="this")
	protected View menuMark;
	
	@ViewOnId(id=R.id.counter_nofity)
	protected TextView notifyCounter;
	@ViewOnId(id=R.id.counter_task)
	protected TextView taskCounter;
	@ViewOnId(id=R.id.counter_trash)
	protected TextView trashCounter;
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case WORKER_OK:
				Const.log("startup 提示加载完成：" + countMap);
				Integer count = countMap.get("notify");
				if(count != null && count > 0){
					notifyCounter.setText(count+"");
					notifyCounter.setVisibility(View.VISIBLE);
				}else{
					notifyCounter.setVisibility(View.GONE);
				}
				
				count = countMap.get("task");
				if(count != null && count > 0){
					taskCounter.setText(count+"");
					taskCounter.setVisibility(View.VISIBLE);
				}else{
					taskCounter.setVisibility(View.GONE);
				}
				
				count = countMap.get("trashApply");
				if(count != null && count > 0){
					trashCounter.setText(count+"");
					trashCounter.setVisibility(View.VISIBLE);
				}else{
					trashCounter.setVisibility(View.GONE);
				}
				break;
			case WORKER_FAIL:
				toast(Const.ERROR);
				break;
			}
		}
	};
	
	@Override
	protected void initUI() {
		super.initUI();
		
		setNavTitle("云办公平台");
		showNavigateBackButton(false);
		
		if(OfficeApplication.isSaleManLogin()){
			menuMember.setVisibility(View.VISIBLE);
		}else{
			menuMember.setVisibility(View.GONE);
		}
	}


	@Override
	public void onClick(View arg0) {
		NerveActivityGroup group = getActivityGroup(IndexGroupActivity.class);
		switch(arg0.getId()){
		case R.id.menuBus:
			group.changeTab(IndexGroupActivity.DEPARTMENT);
			break;
		case R.id.menuHotel:
			group.changeTab(IndexGroupActivity.NOTIFY);
			break;
		case R.id.menuTravel:
			group.changeTab(IndexGroupActivity.TASK);
			break;
		case R.id.menuTrashApply:
			User user = OfficeApplication.USER;
			if(user == null){
				toast("请先登录！");
				return ;
			}
			if(user.getPosition() == User.EMPLOYEE){
				toast("你没有权限进行此操作");
				return ;
			}
			
			group.changeTab(IndexGroupActivity.TRASH_APPLY);
			break;
		case R.id.menuCustomer:
			group.changeTab(IndexGroupActivity.CUSTOMER);
			
			break;
			
		case R.id.menuMark:
			group.changeTab(IndexGroupActivity.CHART);
			break;
		default:
			toast("此功能在演示版本中不能使用");
			break;
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		SharedPreferences sp = getSharedPreferences(AutoLoginActivity.SHARED_FILE, MODE_PRIVATE);
		int lid = sp.getInt(Const.LAST_NOTIFY_ID, 0);
		
		new GetNotifyWorker().execute(lid);
	}
	
	private class GetNotifyWorker extends AsyncTask<Integer, Void, Integer>{

		@SuppressWarnings("unchecked")
		@Override
		protected Integer doInBackground(Integer... params) {
			Reply re = NetService.get(Const.HOST + "getNotify.htm?notifyId=" + params[0]);
			if(re.isError()){
				Const.ERROR = re.getMessage().toString();
				return WORKER_FAIL;
			}
			
			countMap = JSON.parseObject(re.getMessage().toString(), HashMap.class);
			return WORKER_OK;
		}
		
		
		@Override
		protected void onPostExecute(Integer result) {
			handler.sendEmptyMessage(result);
		}
	}
}